package com.im

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.core.graphics.createBitmap

// 用户列表视图
class UserListView {
    // 列表项
    inner class UserListItem(val userName: String, var colorful: Boolean, val normalIcon: ImageBitmap, val grayIcon: ImageBitmap)

    private val mCallback: (String) -> Unit                             // 回调
    private var mResources: Resources                                   // 资源
    private lateinit var mUserInfoList: SnapshotStateList<UserListItem> // 用户信息列表

    /**
     * 构造方法。
     * @param callback  回调
     * @param resources 资源
     */
    constructor(callback: (String) -> Unit, resources: Resources) {
        mCallback = callback
        mResources = resources
    }

    /**
     * 添加一个表项。
     * @param itemText  表项文字
     * @param iconIndex 图标索引
     * @param colorful  彩色则为 true，灰色则为 false
     */
    fun addItem(itemText: String, iconIndex: Int, colorful: Boolean) {
        val bitmap = BitmapFactory.decodeResource(mResources, iconIndex + R.mipmap.normal00)
        val normalIcon = bitmap.asImageBitmap()
        val grayIcon = this.toGrayImage(bitmap).asImageBitmap()
        val item = UserListItem(itemText, colorful, normalIcon, grayIcon)
        mUserInfoList.add(item)
    }

    /**
     * 改变某个表项的图标颜色。
     * @param itemText  表项的文字
     * @param colorful  彩色则为 true，灰色则为 false
     */
    fun changeItemColor(itemText: String, colorful: Boolean) {
        val item: UserListItem? = mUserInfoList.find {
            it.userName == itemText
        }
        if (item != null) {
            val index = mUserInfoList.indexOf(item)
            val newItem = UserListItem(item.userName, colorful, item.normalIcon, item.grayIcon)
            mUserInfoList[index] = newItem
        }
    }

    /**
     * 清除所有表项。
     */
    fun clear() {
        mUserInfoList.clear()
    }

    /**
     * 界面布局。
     */
    @Composable
    fun Greeting() {
        // 初始化成员
        mUserInfoList = remember {
            mutableStateListOf()
        }

        val bitmap = BitmapFactory.decodeResource(mResources, R.mipmap.normal00)
        val iconHeight = bitmap.height.toFloat() / LocalDensity.current.density

        // 创建列表控件
        LazyColumn(Modifier.fillMaxWidth()) {
            itemsIndexed(mUserInfoList) { index, item ->
                Row(
                    Modifier
                        .fillMaxWidth()
                        .background(Const.USER_LIST_VIEW_ITEM_COLOR)
                        .clickable {
                            mCallback(item.userName)
                        }
                ) {
                    Image(if (item.colorful) item.normalIcon else item.grayIcon, "")
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(iconHeight.dp)
                            .padding(start = Const.USER_LIST_VIEW_ITEM_PADDING),
                        verticalArrangement = Arrangement.Center
                    ) {
                        Text(item.userName)
                    }
                }
                Spacer(Modifier
                    .fillMaxWidth()
                    .height(Const.USER_LIST_VIEW_ITEM_MARGIN))
            }
        }
    }

    /**
     * 将彩色图片转换为灰色图片。
     * @param image 图片
     * @return 转换后的图片
     */
    private fun toGrayImage(image: Bitmap): Bitmap {
        // 获取图片的尺寸
        val width = image.width
        val height = image.height

        // 通过位图的大小创建像素点数组
        val pixels = IntArray(width * height)
        image.getPixels(pixels, 0, width, 0, 0, width, height)

        // 通过位移运算得到灰色图片的像素数据
        val alpha = 0xFF shl 24
        for (i in 0 ..< height) {
            for (j in 0 ..< width) {
                var gray = pixels[width * i + j]
                val red = (gray and 0x00FF0000) shr 16
                val green = (gray and 0x0000FF00) shr 8
                val blue = gray and 0x000000FF
                gray = (red * 0.3 + green * 0.59 + blue * 0.11f).toInt()
                gray = alpha or (gray shl 16) or (gray shl 8) or gray
                pixels[width * i + j] = gray
            }
        }

        // 创建灰色图片
        val result = createBitmap(width, height)

        // 设置灰色图片的像素数据
        result.setPixels(pixels, 0, width, 0, 0, width, height)
        return result
    }
}
